package controller.AlumniManagement;

import entity.Alumni;
import entity.AlumniComment;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.AlumniManagement.AlumniService;
import util.ResponseMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/Alumni")
public class AlumniController {

    @Autowired
    private AlumniService alumniService;

    /**
     * 显示校友会活动
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/Show_alumni")
    public ResponseMessage Show_alumni(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<LinkedHashMap<String, Object>> list = alumniService.showAlumni(Current_logged_in_user.getUserId());
//        List<LinkedHashMap<String,Object>> list = alumniService.showAlumni("15171545538");
        if (list.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "当前没有校友会");
        }
        for (int i = 0; i < list.size(); i++) {
            try {
                list.get(i).put("time", String.valueOf(list.get(i).get("time")));
                list.get(i).put("date", String.valueOf(list.get(i).get("date")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", list);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @GetMapping("/one_alumni")
    public ResponseMessage one_alumni(String AlumniId, HttpServletRequest request) {
        if (AlumniId.isEmpty()){
            return new ResponseMessage(ResponseMessage.ERROR, "AlumniId不能为空");
        }
        LinkedHashMap<String, Object> map = alumniService.showOne_alumni(AlumniId);
        if (map.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "没有这个校友会");
        }
        try {
            map.put("time", String.valueOf(map.get("time")));
            map.put("date", String.valueOf(map.get("date")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", map);
    }


    /**
     * 发布校友会
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/issue_alumni")
    public ResponseMessage issue_alumni(String name, String describe, String time, String address, String date, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        if (Current_logged_in_user.getType() != 2) {
            return new ResponseMessage(ResponseMessage.ERROR, "当前用户非校友用户，不能发布校友会活动");
        }
        Alumni alumni = new Alumni();
        alumni.setName(name);
        alumni.setDescribe(describe);
        alumni.setTime(time);
        alumni.setAddress(address);
        alumni.setUserId(Current_logged_in_user.getUserId());
        alumni.setDate(date);
        boolean flag = alumniService.addAlumni(alumni);
        if (flag) {
            return new ResponseMessage(ResponseMessage.SUCCESS, "发布成功");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }

    /**
     * 发布校友会评论
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/alumniComment")
    public ResponseMessage alumniComment(int AlumniId, String comment, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        AlumniComment alumniComment = new AlumniComment();
        alumniComment.setAlumniId(AlumniId);
        alumniComment.setComment(comment);
        //commentTime
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        alumniComment.setCommentTime(df.format(new Date()));
        //commentUserId
        alumniComment.setCommentUserId(Current_logged_in_user.getUserId());
//        alumniComment.setCommentUserId("15072456855");
        alumniService.addAlumniComment(alumniComment);
        return new ResponseMessage(ResponseMessage.SUCCESS, "发布成功");
    }

    /**
     * 显示校友会下所有评论
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/show_alumniComment")
    public ResponseMessage show_alumniComment(int AlumniId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<LinkedHashMap<String, Object>> map = alumniService.selectAlumniComment(AlumniId);
        for (int i = 0; i < map.size(); i++) {
            try {
                map.get(i).put("commentTime", String.valueOf(map.get(i).get("commentTime")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", map);
    }

}//end class
